<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="path" value="${pageContext.request.contextPath }" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>系统资源添加页面</title>
<script type="text/javascript" src="${path}/js/scripts/bootstart.js"></script>
<link rel="stylesheet" type="text/css" href="${path}/css/focus.css" />
</head>
<body style="width: 98%; height: 90%;">
	<form id="resourceForm" method="post">
		<input name="sourceId" class="mini-hidden" id="sourceid" /> <input
			class="mini-hidden" id="valid" />
		<div class="form_nav">
			<table width="100%" cellpadding="4" cellspacing="0">
				<tr>
					<td style="width: 20%;" class="xx_bt nonebor">资源名称:</td>
					<td style="width: 80%;" class="xx_type  nonebor"><input
						name="sourceName" class="mini-textbox" required="true"
						style="width: 100%;" /></td>
				</tr>
				<tr>
					<td class="xx_bt nonebor">父级资源名:</td>
					<td class="xx_type nonebor"><input class="mini-treeselect"
						url="${path }/resource/mgr/showList" parentField="parentId"
						valueField="sourceId" textField="sourceName" name="parentId"
						dataField="list" expandOnLoad="true"
						onnodeclick="resourceform.nodeSelected" id="treeName"
						style="width: 90%;" /> <a class="mini-button"
						onclick="resourceform.clearParentNode">清空</a> <input id="leaf"
						class="mini-hidden" /><input id="sourcelevel" name="sourceLevel"
						value="0" class="mini-hidden" style="display: none;" /></td>
				</tr>
				<!-- <tr style="display:">
						<td align="right">资源类型:</td>
						<td><select name="sourceType"
							id="sourcetype" class="mini-radiobuttonlist">
								<option value="0">后台资源</option>
								<option value="1">前台资源</option>
						</select></td>
					</tr> -->
				<tr>
					<td class="xx_bt nonebor">是否启用:</td>
					<td class="xx_type nonebor"><select name="isUsed" id="isused"
						class="mini-radiobuttonlist">
							<option value="1">启用</option>
							<option value="0">停用</option>
					</select></td>
				</tr>
				<tr id="path">
					<td class="xx_bt nonebor">路径:</td>
					<td class="xx_type nonebor"><input name="path" id="resourcePath" style="width: 100%;"
						class="mini-textbox" onvalidation="resourceform.getResourceKey" /></td>
				</tr>
				<tr>
					<td class="xx_bt nonebor">资源标识符:</td>
					<td class="xx_type nonebor"><input name="target" id="flagkey" style="width: 100%;"
						class="mini-textbox"/></td>
				</tr>
				<tr>
					<td class="xx_bt nonebor">选择功能:</td>
					<td class="xx_type nonebor"><input id="fid" name="functionIds" class="mini-buttonedit"
						onbuttonclick="onButtonEdit" allowInput="false"
						style="width: 90%;" /> <a class="mini-button"
						onclick="resourceform.functionId">清空</a></td>
				</tr>
				<tr>
					<td class="xx_bt nonebor">功能名称:</td>
					<td class="xx_type nonebor"><input id="functionName" name="functionNames"
						class="mini-textarea" style="width: 100%; height: 100px;"
						readonly="readonly" /></td>
				</tr>
			</table>
		</div>
		<div style="text-align: center; padding: 10px;">
			<a class="mini-button" onclick="resourceform.saveData"
				style="width: 60px; margin-right: 20px;">保存</a> <a
				class="mini-button" onclick="$.form.onCancel()" style="width: 60px;">取消</a>
		</div>
	</form>
</body>
<script type="text/javascript" src="${path}/js/scripts/dataform.js"></script>
<script type="text/javascript">
	mini.parse();
	$(function() {
		$.form.init(); //初始化表单
		window.resourceform = {};
		resourceform.saveData = function() {
			$.form.saveData({
				url : "${path}/resource/mgr/addResource",
				id : "resourceForm",
				showMsg : true,
				validator : function() { //业务验证
					var valid = mini.get("valid"); //显示选择修改父类树的错误信息
					if (valid.getValue()) {
						mini.alert(valid.getValue());
						return false;
					}
					return true;
				}
			});
		};
		resourceform.getResourceKey = function(e) {
			if (!e.value) {
				return;
			}
			var value = e.value;
			var pattern = /^[\/]|^[http]/;
			if (!pattern.test(value)) {
				e.errorText = "路径必须以/或http开头";
				e.isValid = false;
				return;
			}
			if (value.lastIndexOf(".") != -1) {
				value = value.substring(value.lastIndexOf("/"), value
						.lastIndexOf("."));
			} else {
				value = value.substring(value.lastIndexOf("/"));
			}

			if (value.lastIndexOf("!") != -1) {
				value = value.substring(0, value.lastIndexOf("!"));
			}

			mini.get("flagkey").setValue(value);
		};

		/**
		 * 获取选中的节点id值
		 */
		resourceform.nodeSelected = function(e) {
			var node = e.node; //获取选中的节点

			//选中父类节点与自己进行比较
			var selfId = mini.get("sourceid");
			var valid = mini.get("valid"); //存放错误信息
			if (node.sourceid == selfId.getValue()) {
				valid.setValue("不能选择自己作为父类资源！");
				return;
			}

			//判断是否选中当前节点子节点作为父节点修改
			var iterParentNode = function(n) {
				if (selfId.getValue() > 0 && n.parentid == selfId.getValue()) {
					valid.setValue("不能选择自己的子类，作为父类资源！");
					return false;
				}

				//获取当前节点的父类节点
				var pNode = e.sender.getParentNode(n);
				if (pNode) {
					return iterParentNode(pNode);
				}
				return true;
			};

			if (!iterParentNode(node)) { //如果选择了当前节点的子节点
				return;
			}
			valid.setValue("");
			//获取选中的id值
			var sourcelevel = mini.get("sourcelevel");
			//mini.get("parentid").setValue(node.sourceid);
			//mini.get("parentkey").setValue(node.flagkey);
			sourcelevel.setValue(node._level + 1);
			var sourcetype = mini.get("sourcetype");
			if (node.children) { //选中节点是否存在子元素
				mini.get("leaf").setValue("false"); //表示该节点不是叶子节点
			} else {
				mini.get("leaf").setValue("true");
			}
		};

		//页面初始化时，执行该函数资源类型
		$.form.loadFormSuccess = function() {
			//var sourcetype = mini.get("sourcetype");
			//resourceform.resourceFlag(sourcetype);
			var treeobj = mini.get("treeName");
			var data = treeobj.getList();
			if (data) {
				for (var i = 0, a = data.length; i < a; i++) {
					if (data[i].sourceId == treeobj.value) {
						treeobj.setText(data[i].sourceName);
						break;
					}
				}
			}
			mini.get("fid").setText(mini.get("fid").value);
		};
		resourceform.clearParentNode = function() {
			mini.get("sourcelevel").setValue("0");
			mini.get("treeName").setValue("");
		};
		resourceform.functionId = function() {
			mini.get("fid").setValue("");
			mini.get("fid").setText("");
			mini.get("functionName").setValue("");
		};
	});
	//选择功能
	function onButtonEdit(e) {
		if (mini.get("sourcelevel").value < 1) {
			mini.alert("功能按钮不能添加在一级节点上！");
			return;
		}
		var btnEdit = this;
		var idString = btnEdit.getValue();
		mini.open({
			url : "${path}/function/mgr/functionselect?idString="
					+ idString,
			title : "选择功能",
			//height : doc.height()+50,
			ondestroy : function(action) {
				//if (action == "close") return false;
				if (action == "ok") {
					var iframe = this.getIFrameEl();
					var data = iframe.contentWindow.GetData();
					data = mini.clone(data); //必须
					if (data && data.id.length > 0) {
						btnEdit.setValue(data.id);
						btnEdit.setText(data.id);
						mini.get("functionName").setValue(data.text);
					}
				}
			}
		});
	}
</script>
</html>